﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AD.Web.Common;
using AD.Contract;
using AD.Contract.Models;
using HZT.Framework.Contract;

namespace AD.Web.Areas.BaseInfo.Controllers
{
    public class DeptController : ADController
    {
        //
        // GET: /BaseInfo/Dept/
        //广告部门、部门任务的相关操作
        public ActionResult Index(CommonRequest request)
        {
            //获取部门列表
           // var request = new CommonRequest();

            var result = this.DeptService.GetDeptList(request);
            return View(result);
        }
        //新增
        public ActionResult Create()
        {
            var model = new Department(); 
            return View("Edit", model);
        }
        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            var  model = new Department();

            this.TryUpdateModel<Department>(model, new string[]{"Id","Name","Sort"});
            try
            {
                this.DeptService.SaveDepartment(model);
            }
            catch (BusinessException e)
            {
                this.ModelState.AddModelError(e.Name, e.Message);
                return View("Edit", model);
            }

            return this.RefreshParent();
        }
        //编辑
        public ActionResult Edit(string  Id)
        {
            var model = this.DeptService.GetDept(Id);
         
            return View(model);
        }
        [HttpPost]
        public ActionResult Edit(string Id, FormCollection collection)
        {
            var model = this.DeptService.GetDept(Id);
            this.TryUpdateModel<Department>(model);
            try
            {
                this.DeptService.SaveDepartment(model);
            }
            catch (BusinessException e)
            { 
                this.ModelState.AddModelError(e.Name, e.Message);
               // return View("Edit", model);
            }

            return this.RefreshParent();
        }
    }
}
